#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i ++) cin >> a[i];

  vector<vector<i64>> dp(n + 2, vector<i64>(n + 2));
  for (int l = n; l >= 1; l --) {
    for (int r = l; r <= n; r ++) {
      for (int k = l; k < r; k ++) {
        dp[l][r] = max(dp[l][r], dp[l][k] + dp[k + 1][r]);
      }
      if ((r - l + 1) % 3 == 0) {
        for (int k = l + 1; k < r; k += 3) {
          dp[l][r] = max(dp[l][r], dp[l+1][k-1] + dp[k+1][r-1] + a[l] + a[k] + a[r]);
        }
      }
    }
  }
  cout << dp[1][n] << endl;
}
